﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using scheduleMVC.Models;
using scheduleMVC.DAL;
using System.Web.Security;

namespace scheduleMVC.Controllers
{
    public class CustomerController : Controller
    {
        private CustomerContext db = new CustomerContext();
        private ApplicationDbContext db2 = new ApplicationDbContext();

        // GET: /Customer/
        public ActionResult Index()
        {


          
            return View(db.Customers.ToList());
        }

        // GET: /Customer/Details/5
        public ActionResult Details(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CustomerModel customermodel = db.Customers.Find(id);
            if (customermodel == null)
            {
                return HttpNotFound();
            }
            return View(customermodel);
        }




        private void GetUsers()
        {
            User.IsInRole("");
            ViewBag.Users =
             (from Users 
                  in db2.Users.ToArray() 
            // where Roles.GetRolesForUser(Users.UserName).FirstOrDefault().ToString() == "Customer"
            // where (Roles.IsUserInRole(Users.UserName,"Customer") == true)
              select new SelectListItem
              {
                  Text = Users.UserName,
                  Value = Users.Id
              }).ToArray();
        }

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        // GET: /Customer/Create
        public ActionResult Create()
        {
            GetUsers();
            return View();
        }

        // POST: /Customer/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="CustomerId,UserId,Name_Prefix,Last_Name,Middle_name,First_Name,Business_Name,LegalName,Phone_Number,Cell_Phone,Fax,Email,Customer_Account")] CustomerModel customermodel)
        {
            if (ModelState.IsValid)
            {
                db.Customers.Add(customermodel);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(customermodel);
        }

        // GET: /Customer/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CustomerModel customermodel = db.Customers.Find(id);
            if (customermodel == null)
            {
                return HttpNotFound();
            }
            return View(customermodel);
        }

        // POST: /Customer/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="CustomerId,UserId,Name_Prefix,Last_Name,Middle_name,First_Name,Business_Name,LegalName,Phone_Number,Cell_Phone,Fax,Email,Customer_Account")] CustomerModel customermodel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(customermodel).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customermodel);
        }

        // GET: /Customer/Delete/5
        public ActionResult Delete(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CustomerModel customermodel = db.Customers.Find(id);
            if (customermodel == null)
            {
                return HttpNotFound();
            }
            return View(customermodel);
        }

        // POST: /Customer/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CustomerModel customermodel = db.Customers.Find(id);
            db.Customers.Remove(customermodel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
